/*
 * Copyright (C) 2025 Tsingteng MicroSystem
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * @file nci_nfccc_impl_tmsnci.cpp
 * @brief NFCC部分接口实现
 */

#include "nci_nfcc_impl_tmsnci.h"
#include "nci_tag_impl_tmsnci.h"
#include "nci_ce_impl_tmsnci.h"
#include "nfa_dm.h"
#include "nfa_discover.h"
#include "nfc_log_api.h"

namespace OHOS {
namespace NFC {
namespace NCI {

static void nci_ce_cback(tNCI_CE_EVT event, tNCI_CE_INFO *p_info) {
    NciCeImplTmsNci::GetInstance().CeEventCallback(event, p_info);
}

static void nci_rw_cback(tNCI_RW_EVT event, const tNCI_RW_TAG *p_tag) {
    NciTagImplTmsNci::GetInstance().RwEventCallback(event, p_tag);
}

NciNfccImplTmsNci::NciNfccImplTmsNci() {
}

NciNfccImplTmsNci &NciNfccImplTmsNci::GetInstance() {
    static NciNfccImplTmsNci nfccImpl;
    return nfccImpl;
}

bool NciNfccImplTmsNci::Initialize()
{
    tNCI_INIT_CFG init_cfg = {
        .rw_cback = nci_rw_cback,
        .ce_cback = nci_ce_cback,
    };
    tNCI_STATUS ret = nfa_enable(&init_cfg);
    if (ret != NCI_STATUS_OK) {
        NFCLOG_E("nfa_enable failed, ret=%d", ret);
    }
    return ret == NCI_STATUS_OK;
}
bool NciNfccImplTmsNci::Deinitialize() {
    tNCI_STATUS ret = nfa_disable(false);
    if (ret != NCI_STATUS_OK) {
        NFCLOG_E("nfa_disable(close) failed, ret=%d", ret);
    }
    return ret == NCI_STATUS_OK;
}
void NciNfccImplTmsNci::EnableDiscovery(uint16_t techMask, bool enableReaderMode, bool enableHostRouting, bool restart) {
    tNCI_STATUS ret = nfa_rf_enable_discovery(techMask, enableReaderMode, restart);
    if (ret != NCI_STATUS_OK) {
        NFCLOG_E("nfa_rf_enable_discovery failed, ret=%d", ret);
    }
}
void NciNfccImplTmsNci::DisableDiscovery() {
    tNCI_STATUS ret = nfa_rf_stop_discovery();
    if (ret != NCI_STATUS_OK) {
        NFCLOG_E("nfa_rf_stop_discovery failed, ret=%d", ret);
    }
}
bool NciNfccImplTmsNci::SetScreenStatus(uint8_t screenStateMask) {
    tNCI_STATUS ret = nfa_set_screen_state(screenStateMask, false);
    if (ret != NCI_STATUS_OK) {
        NFCLOG_E("nfa_set_screen_state failed, ret=%d", ret);
    }
    return ret == NCI_STATUS_OK;
}
int NciNfccImplTmsNci::GetNciVersion() {
    tNCI_VERSION version = nfa_get_nci_version();
    NFCLOG_I("nci version=0x%02X", version);
    return version;
}
void NciNfccImplTmsNci::Abort() {
    NFCLOG_E("abort");
    _exit(0);
}
void NciNfccImplTmsNci::FactoryReset() {
    tNCI_STATUS ret = nfa_factory_reset();
    if (ret != NCI_STATUS_OK) {
        NFCLOG_E("nfa_factory_reset failed, ret=%d", ret);
    }
}
void NciNfccImplTmsNci::Shutdown() {
    tNCI_STATUS ret = nfa_disable(true);
    if (ret != NCI_STATUS_OK) {
        NFCLOG_E("nfa_disable(shutdown) failed, ret=%d", ret);
    }
}

}  // namespace NCI
}  // namespace NFC
}  // namespace OHOS